﻿using ChurchDiary.Common;
using ChurchDiary.DAL.Contract;
using ChurchDiary.DAL.DAL;
using ChurchDiary.TableEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChurchDiary.DAL.Methods 
{
    public class DAL_Organization : DataAccess, IOrganization
    {
        public string GetOrganizationProfile()
        {
            string OutPutJson = "";
            using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationStructure]"))
            {
                using (DbConnection con = SQLServer.CreateConnection())
                {
                    using (IDataReader MReader = SQLServer.ExecuteReader(dbcommand))
                    {
                        if (null == MReader)
                        {
                            return OutPutJson;
                        }
                        else
                        {
                            while (MReader.Read())
                            {
                                OutPutJson = MReader["jsonoutput"].ToString();
                            }
                        }
                    }
                }
            }
            return OutPutJson;
        }
        public int InsertOrganizationStructure(OrganizationStructure organization)
        {
            int IResult = 0;
            try
            {
                for (int i = 1; i <= organization.OrganizationTitle.Count - 1; i++)
                {
                    using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IUD_InsertOrganizationStructure]"))
                    {

                        SQLServer.AddInParameter(dbCommand, "@Organiztionid", DbType.Int32, organization.OrganizationTitle[i].key);
                        SQLServer.AddInParameter(dbCommand, "@OrganizationName", DbType.String, organization.OrganizationTitle[i].name);
                        SQLServer.AddInParameter(dbCommand, "@OrganizationAbrivation", DbType.String, organization.OrganizationTitle[i].title);
                        SQLServer.AddInParameter(dbCommand, "@ParentOrganization", DbType.Int32, organization.OrganizationTitle[i].parent);
                        using (DbConnection con = SQLServer.CreateConnection())
                        {
                            IResult = (int)SQLServer.ExecuteScalar(dbCommand);
                            IResult = +1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return IResult;
        }
        public List<Departments> GetDepartMents()
        {
            List<Departments> departments = new List<Departments>();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_ChurchDepartMents]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if(OReader==null)
                            {
                                return departments;
                            }
                            while (OReader.Read())
                            {
                                Departments Odepartment = new Departments();
                                Odepartment.DepartmentName = OReader["OrganizationName"].ToString();
                                Odepartment.Departmentsid = (int)OReader["Organiztionid"];
                                departments.Add(Odepartment);
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return departments;
        }
        public AllDepartMentDetails GetSubDepartMents(int Parent,int Mode)
        {
            AllDepartMentDetails AllDepartMent = new AllDepartMentDetails();
            try
            {
                List<EnitityOrganizationPartition> OrganizationPartition = new List<EnitityOrganizationPartition>();
                List<EntityOrganizationProfile> Profilelist = new List<EntityOrganizationProfile>();
                using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationPartitionDetails]"))
                {
                    SQLServer.AddInParameter(dbcommand, "@parentId", DbType.Int32, Parent);
                    if (Mode == 1)
                    {
                        SQLServer.AddInParameter(dbcommand, "@mode", DbType.String, GetDataType.LIST);
                    }
                    else
                    {
                        SQLServer.AddInParameter(dbcommand, "@mode", DbType.String, GetDataType.ALL);
                    }
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader MReader = SQLServer.ExecuteReader(dbcommand))
                        {
                            if (null == MReader)
                            {
                                return AllDepartMent;
                            }
                            else
                            {
                                while (MReader.Read())
                                {
                                    EntityOrganizationProfile Profile = new EntityOrganizationProfile();
                                    Profile.id = (int)MReader["id"];
                                    Profile.Organiztionid = (int)MReader["Organiztionid"];
                                    Profile.OrganizationName = MReader["OrganizationName"].ToString();
                                    Profile.OrganizationAbrivation = MReader["OrganizationAbrivation"].ToString();
                                    Profile.ParentOrganization = (int)MReader["ParentOrganization"];
                                    Profile.IsActive =(bool) MReader["IsActive"];
                                    Profile.IsMain = (bool)MReader["IsMain"];
                                    Profilelist.Add(Profile);
                                }
                                AllDepartMent.OrganizationProfile = Profilelist;
                                MReader.NextResult();
                                while (MReader.Read())
                                {
                                    EnitityOrganizationPartition OrgPartition = new EnitityOrganizationPartition();
                                    OrgPartition.PartitionId = (Guid)MReader["PartitionId"];
                                    OrgPartition.PartitionName = MReader["PartitionName"].ToString();
                                    OrgPartition.OrganizationId = (int)MReader["OrganizationLevel"];
                                    OrganizationPartition.Add(OrgPartition);
                                }
                                AllDepartMent.OrganizationPartition = OrganizationPartition;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return AllDepartMent;
        }
        #region Organization Partition
        public string InsertOrganizationPartition(EnitityOrganizationPartition OrganizationPartition)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_OrganizationPartition]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@PartitionId", DbType.Guid, OrganizationPartition.PartitionId);
                    SQLServer.AddInParameter(dbCommand, "@PartitionName", DbType.String, CommonFunction.UppercaseFirst(OrganizationPartition.PartitionName));
                    SQLServer.AddInParameter(dbCommand, "@PartitionAbrivation", DbType.String, OrganizationPartition.PartitionAbrivation);
                    SQLServer.AddInParameter(dbCommand, "@OrganizationId", DbType.Int32, OrganizationPartition.OrganizationId);
                    SQLServer.AddInParameter(dbCommand, "@OrganizationLevel", DbType.Int32, OrganizationPartition.OrganizationLevel);
                    SQLServer.AddInParameter(dbCommand, "@ParentPartitionId", DbType.Guid, OrganizationPartition.ParentPartitionId);
                    SQLServer.AddInParameter(dbCommand, "@By", DbType.Guid, Current.Userid);
                    SQLServer.AddInParameter(dbCommand, "@On", DbType.DateTime, DateTime.Now);
                    SQLServer.AddInParameter(dbCommand, "@Status", DbType.Int32, 1);
                    SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, eMode.INS);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public string UpdateOrganizationPartition(EnitityOrganizationPartition OrganizationPartition)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_OrganizationPartition]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@PartitionId", DbType.Guid, OrganizationPartition.PartitionId);
                    SQLServer.AddInParameter(dbCommand, "@PartitionName", DbType.String, OrganizationPartition.PartitionName);
                    SQLServer.AddInParameter(dbCommand, "@PartitionAbrivation", DbType.String, OrganizationPartition.PartitionAbrivation);
                    SQLServer.AddInParameter(dbCommand, "@OrganizationId", DbType.Int32, OrganizationPartition.OrganizationId);
                    SQLServer.AddInParameter(dbCommand, "@OrganizationLevel", DbType.Int32, OrganizationPartition.OrganizationLevel);
                    SQLServer.AddInParameter(dbCommand, "@ParentPartitionId", DbType.Guid, OrganizationPartition.ParentPartitionId);
                    SQLServer.AddInParameter(dbCommand, "@By", DbType.Guid, Current.Userid);
                    SQLServer.AddInParameter(dbCommand, "@On", DbType.DateTime, DateTime.Now);
                    SQLServer.AddInParameter(dbCommand, "@Status", DbType.Int32, OrganizationPartition.Status);
                    SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, eMode.UPD);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public string DeleteOrganizationPartition(Guid PartitionId)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_OrganizationPartition]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@PartitionId", DbType.Guid, PartitionId);
                    SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, eMode.DEL);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public List<EnitityOrganizationPartition> GetOrganizationPartitions()
        {
            List<EnitityOrganizationPartition> OrganizationPartitions = new List<EnitityOrganizationPartition>();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationPartition]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, GETMODE.LIST);
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return OrganizationPartitions;
                            }
                            while (OReader.Read())
                            {
                                EnitityOrganizationPartition OrganizationPartition = new EnitityOrganizationPartition();
                                OrganizationPartition.PartitionId = (Guid)OReader["PartitionId"];
                                OrganizationPartition.PartitionName = OReader["PartitionName"].ToString();
                                OrganizationPartition.PartitionAbrivation = OReader["PartitionAbrivation"].ToString();
                                OrganizationPartition.OrganizationId = (int)OReader["OrganizationId"];
                                OrganizationPartition.OrganizationLevel = (int)OReader["OrganizationLevel"];
                                OrganizationPartition.ParentPartitionId = (Guid)OReader["ParentPartitionId"];
                                OrganizationPartition.CreatedBy = (Guid)OReader["CreatedBy"];
                                OrganizationPartition.CreatedOn = (DateTime)OReader["CreatedOn"];
                                OrganizationPartition.Status = (int)OReader["Status"];
                                OrganizationPartition.Org = new EntityOrg();
                                OrganizationPartition.Org.OrganizationName = OReader["OrganizationName"].ToString();
                                OrganizationPartition.Org.ParentPartition = OReader["parentPartition"].ToString();
                                OrganizationPartitions.Add(OrganizationPartition);
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return OrganizationPartitions;
        }
        public List<EnitityOrganizationPartition> GetOrganizationPartitionfordp()
        {
            List<EnitityOrganizationPartition> OrganizationPartitions = new List<EnitityOrganizationPartition>();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationPartition]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, "FORDP");
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return OrganizationPartitions;
                            }
                            while (OReader.Read())
                            {
                                EnitityOrganizationPartition OrganizationPartition = new EnitityOrganizationPartition();
                                OrganizationPartition.PartitionId = (Guid)OReader["PartitionId"];
                                OrganizationPartition.PartitionName = OReader["PartitionName"].ToString();
                                OrganizationPartition.PartitionAbrivation = OReader["PartitionAbrivation"].ToString();
                                OrganizationPartition.OrganizationId = (int)OReader["OrganizationId"];
                                OrganizationPartition.OrganizationLevel = (int)OReader["OrganizationLevel"];
                                OrganizationPartition.ParentPartitionId = (Guid)OReader["ParentPartitionId"];
                                OrganizationPartition.CreatedBy = (Guid)OReader["CreatedBy"];
                                OrganizationPartition.CreatedOn = (DateTime)OReader["CreatedOn"];
                                OrganizationPartition.Status = (int)OReader["Status"];
                                OrganizationPartition.Org = new EntityOrg();
                                OrganizationPartition.Org.OrganizationName = OReader["OrganizationName"].ToString();
                                OrganizationPartition.Org.ParentPartition = OReader["parentPartition"].ToString();
                                OrganizationPartitions.Add(OrganizationPartition);
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return OrganizationPartitions;
        }
        public List<EnitityOrganizationPartition> GetOrganizationPartitions(int OrganizationId)
        {
            List<EnitityOrganizationPartition> OrganizationPartitions = new List<EnitityOrganizationPartition>();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationPartition]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, GETMODE.SEARCH);
                        SQLServer.AddInParameter(dbCommand, "@Organizationid", DbType.Int32, OrganizationId);
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return OrganizationPartitions;
                            }
                            while (OReader.Read())
                            {
                                EnitityOrganizationPartition OrganizationPartition = new EnitityOrganizationPartition();
                                OrganizationPartition.PartitionId = (Guid)OReader["PartitionId"];
                                OrganizationPartition.PartitionName = OReader["PartitionName"].ToString();
                                OrganizationPartition.PartitionAbrivation = OReader["PartitionAbrivation"].ToString();
                                OrganizationPartition.OrganizationId = (int)OReader["OrganizationId"];
                                OrganizationPartition.OrganizationLevel = (int)OReader["OrganizationLevel"];
                                OrganizationPartition.ParentPartitionId = (Guid)OReader["ParentPartitionId"];
                                OrganizationPartition.CreatedBy = (Guid)OReader["CreatedBy"];
                                OrganizationPartition.CreatedOn = (DateTime)OReader["CreatedOn"];
                                OrganizationPartition.Status = (int)OReader["Status"];
                                OrganizationPartition.Org = new EntityOrg();
                                OrganizationPartition.Org.OrganizationName = OReader["OrganizationName"].ToString();
                                OrganizationPartition.Org.ParentPartition = OReader["parentPartition"].ToString();
                                OrganizationPartitions.Add(OrganizationPartition);
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return OrganizationPartitions;
        }
        public EnitityOrganizationPartition GetOrganizationPartition(Guid PartitionId)
        {
            EnitityOrganizationPartition OrganizationPartition = new EnitityOrganizationPartition();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationPartition]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {

                        SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, GETMODE.ONE);
                        SQLServer.AddInParameter(dbCommand, "@PartitionId", DbType.Guid, PartitionId);
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return OrganizationPartition;
                            }
                            while (OReader.Read())
                            {
                                OrganizationPartition.PartitionId = (Guid)OReader["PartitionId"];
                                OrganizationPartition.PartitionName = OReader["PartitionName"].ToString();
                                OrganizationPartition.PartitionAbrivation = OReader["PartitionAbrivation"].ToString();
                                OrganizationPartition.OrganizationId = (int)OReader["OrganizationId"];
                                OrganizationPartition.OrganizationLevel = (int)OReader["OrganizationLevel"];
                                OrganizationPartition.ParentPartitionId = (Guid)OReader["ParentPartitionId"];
                                OrganizationPartition.CreatedBy = (Guid)OReader["CreatedBy"];
                                OrganizationPartition.CreatedOn = (DateTime)OReader["CreatedOn"];
                                OrganizationPartition.Status = (int)OReader["Status"];
                                OrganizationPartition.Org = new EntityOrg();
                                OrganizationPartition.Org.OrganizationName = OReader["OrganizationName"].ToString();
                                OrganizationPartition.Org.ParentPartition = OReader["parentPartition"].ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return OrganizationPartition;
        }
        #endregion
        #region OrganizationInfo
        public EntityOrganizationInfo GetOrganizationInfo()
        {
            EntityOrganizationInfo OrganizationInfo = new EntityOrganizationInfo();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationInfo]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return OrganizationInfo;
                            }
                            while (OReader.Read())
                            {
                                OrganizationInfo.NameOfOrganization = OReader["NameOfOrganization"].ToString();
                                OrganizationInfo.OrganizationAbbrivation = OReader["OrganizationAbbrivation"].ToString();
                                OrganizationInfo.OrganizationAddress = OReader["OrganizationAddress"].ToString();
                                OrganizationInfo.OrganizationEmail = OReader["OrganizationEmail"].ToString();
                                OrganizationInfo.OrganizationId = (Guid)OReader["OrganizationId"];
                                OrganizationInfo.OrganizationLogo = OReader["OrganizationLogo"].ToString();
                                OrganizationInfo.OrganizationMobile = OReader["OrganizationMobile"].ToString();
                                OrganizationInfo.OrganizationPhone = OReader["OrganizationPhone"].ToString();
                                OrganizationInfo.OrganizationWebsite = OReader["OrganizationWebsite"].ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return OrganizationInfo;
        }
        public EntityUser GetUserInfo(Guid? UserId)
        {
            EntityUser User = new EntityUser();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_UserInfo]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@UserId", DbType.Guid, UserId);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return User;
                            }
                            while (OReader.Read())
                            {
                                User.Userid = (Guid)OReader["Userid"];
                                User.UserName = OReader["UserName"].ToString();
                                User.Password = OReader["Password"].ToString();
                                User.Emailid = OReader["Emailid"].ToString();
                                User.Name = OReader["Name"].ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return User;
        }
        public EntitySoftwareInfo GetSoftwareInfo()
        {
            EntitySoftwareInfo SoftwareInfo = new EntitySoftwareInfo();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_SoftwareInfo]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return SoftwareInfo;
                            }
                            while (OReader.Read())
                            {
                                SoftwareInfo.SoftwareId = (Guid)OReader["SoftwareId"];
                                SoftwareInfo.SoftwareLogo = OReader["SoftwareLogo"].ToString();
                                SoftwareInfo.SoftwareName = OReader["SoftwareName"].ToString();
                                SoftwareInfo.SoftwareSupportLink = OReader["SoftwareSupportLink"].ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return SoftwareInfo;
        }
        #endregion

    }
}
